<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="resetFormData">
        添加控制参数
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      row-key="id"
      border
    >
      <el-table-column
        prop="name"
        label="控制说明"
      />
      <el-table-column
        prop="attr"
        label="属性"
      />
      <el-table-column
        prop="val"
        label="值"
      />
      <el-table-column label="操作" width="140" align="center">
        <template slot-scope="{row}">
          <el-button type="danger" @click="deleteFlowParams(row.id)">删除属性</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" style="width: 1100px;margin: auto">
      <el-form ref="dataForm" :rules="rules" :model="fromTemp" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <el-form-item label="控制说明" prop="name">
          <el-input v-model="fromTemp.name" />
        </el-form-item>
        <el-form-item label="属性" prop="attr">
          <el-input v-model="fromTemp.attr" />
        </el-form-item>
        <el-form-item label="值" prop="val">
          <el-input v-model="fromTemp.val" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveFlowParams">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { DeleteFlowParams, SaveFlowParams, GetFlowParamsList } from '@/api/SystemManagerApi'

const optionsMap = {
  'START': '开始节点',
  'NODE': '审批节点',
  'END': '结束节点',
  'FLOW': '子流程'
}

export default {
  filters: {
    exchange(val) {
      return optionsMap[val]
    }
  },
  props: ['flowId'],
  data() {
    return {
      rules: {
        attr: [{ required: true, message: 'type is required', trigger: 'change' }],
        name: [{ required: true, message: 'type is required', trigger: 'change' }],
        val: [{ required: true, message: 'type is required', trigger: 'change' }],
      },
      title: '编辑控制属性',
      fromTemp: {
        id: null,
        name: null,
        flowId: this.flowId,
        attr: null,
        val: null
      },
      dialogFormVisible: false,
      listLoading: false,
      list: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      GetFlowParamsList(this.flowId).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    resetFormData() {
      this.fromTemp = {
        id: null,
        name: null,
        flowId: this.flowId,
        attr: null,
        val: null
      }
      this.dialogFormVisible = true
    },
    deleteFlowParams(flowId) {
      this.$confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        DeleteFlowParams(flowId).then(() => {
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    saveFlowParams() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.listLoading = true
          SaveFlowParams(this.fromTemp).then((response) => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>
